package com.tangkuo.util;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
/**
 * 
 * @author tangkuo
 *
 */
public class LogUtil {
	private static Logger logger = LoggerFactory.getLogger(LogUtil.class);
	
	/**
	 *
	 * @Title: printReqMsgJson
	 * @Description: (打印输出请求对象参数信息,同时敏感字段处理后输出日志)
	 * @param objReq
	 *            Object
	 * @return String
	 */
	public static String printReqMsgJson(Object objReq) {
	  String reqJsonStr = StringUtils.EMPTY;
	  if(objReq instanceof String ){
	    reqJsonStr = (String) objReq;
	  }else {
	    reqJsonStr = JSON.toJSONString(objReq);
	  }
	  return reqJsonStr;
	}
	
	/**
	 * 针对部分字段脱敏处理
	 *
	 * @param map  Map<String, Object>
	 */
	@SuppressWarnings("unused")
	private static void buiderMapInDesField(Map<String, Object> map) {
		for (Object objKey : map.keySet()) {
			//logger.info("key为：" + objKey + "值为：" + map.get(objKey));
			if ("phoneNo".equals(objKey)) {
				String phoneNo = (String) map.get(objKey);
				map.put("phoneNo", formatMobile(phoneNo));
				continue;
			}
			if ("operatorAccountNo".equals(objKey)) {
			  String telPhone = (String) map.get(objKey);
			  map.put("operatorAccountNo", formatMobile(telPhone));
			  continue;
			}
			if ("accNo".equals(objKey)) {
				String accNo = (String) map.get(objKey);
				map.put("accNo", getBankCardFirst6Last4(accNo));
				continue;
			}
			if ("operator".equals(objKey)) {
				String phoneNo = (String) map.get(objKey);
				map.put("operator", formatMobile(phoneNo));
				continue;
			}
			if ("allotUser".equals(objKey)) {
				String phoneNo = (String) map.get(objKey);
				map.put("allotUser", formatMobile(phoneNo));
				continue;
			}
			if ("certNo".equals(objKey)) {
				String certNo = (String) map.get(objKey);
				map.put("certNo", getIdCardFirst3Last4(certNo));
				continue;
			}
			if ("accountName".equals(objKey)) {
				String accountName = (String) map.get(objKey);
				map.put("accountName", getName(accountName));
				continue;
			}if ("operatorName".equals(objKey)) {
				String accountName = (String) map.get(objKey);
				map.put("operatorName", getName(accountName));
				continue;
			}
			if ("customerName".equals(objKey)) {
				String customerName = (String) map.get(objKey);
				map.put("customerName", getName(customerName));
				continue;
			}
			if ("userName".equals(objKey)) {
				String userName = (String) map.get(objKey);
				map.put("userName", getName(userName));
				continue;
			}
			if ("username".equals(objKey)) {
				String username = (String) map.get(objKey);
				map.put("username", getName(username));
				continue;
			}
			if ("realname".equals(objKey)) {
				String realname = (String) map.get(objKey);
				map.put("realname", getName(realname));
				continue;
			}
			if ("loginUser".equals(objKey)) {
			  String loginUser = (String) map.get(objKey);
			  map.put("loginUser", getName(loginUser));
			  continue;
			}
			if ("bankAccountName".equals(objKey)) {
				  String loginUser = (String) map.get(objKey);
				  map.put("bankAccountName", getName(loginUser));
				  continue;
			}
			if ("check_object_name".equals(objKey)) {
				  String loginUser = (String) map.get(objKey);
				  map.put("check_object_name", getName(loginUser));
				  continue;
			}if ("partnerName".equals(objKey)) {
				  String loginUser = (String) map.get(objKey);
				  map.put("partnerName", getName(loginUser));
				  continue;
			}
			if ("cvn2".equals(objKey)) {
				String cvn2 = (String) map.get(objKey);
				map.put("cvn2", formatString(cvn2));
				continue;
			}
			if ("validDate".equals(objKey)) {
				String validDate = (String) map.get(objKey);
				map.put("validDate", formatString(validDate));
				continue;
			}
			if ("password".equals(objKey)) {
				String password = (String) map.get(objKey);
				map.put("password", formatString(password));
				continue;
			}
			if ("createOperator".equals(objKey)) {
				String createOperator = (String) map.get(objKey);
				map.put("createOperator", getName(createOperator));
				continue;
			}
			if ("updateOperator".equals(objKey)) {
				String updateOperator = (String) map.get(objKey);
				map.put("updateOperator", getName(updateOperator));
				continue;
			}
		}
	}
	
	/**
	 *
	 * @Title: formatString
	 * @Description: (将字符串用*替代长度保持不变)
	 * @param str
	 *            String
	 * @return String 返回类型
	 */
	private static String formatString(String str) {
		String returnStr = StringUtils.EMPTY;
		if (StringUtils.isNotBlank(str)) {
			long number = str.length();
			for (int i = 0; i < number; i++) {
				returnStr += "*";
			}
		}
		return returnStr;
	}
	
	/**
	 * 隐藏真实姓名 姓
	 *
	 * @param name
	 * @return
	 */
	public static String getName(String name) {
		String returnName = "";
		if (StringUtils.isNotEmpty(name)) {
			if (name.length() > 2) {
				for(int i=0;i<name.length() -2;i++){
					returnName = returnName + "*";
				}
				returnName  = returnName + name.substring(name.length() - 2, name.length());
			}else if (name.length() == 2) {
			    returnName = "*" + name.substring(name.length() - 1, name.length());
			} else {
				returnName +=  name.substring(name.length() - 1, name.length());
			}
			return returnName;
		}
		return "";
	}
	
	/**
	 * 格式化手机号码
	 *
	 * @param cardNo
	 * @return
	 */
	public static String formatMobile(String mobile) {
		if (StringUtils.isEmpty(mobile)) {
			return "";
		}
		if (mobile.length() <= 7) {
			return mobile;
		}
		StringBuffer sb = new StringBuffer();
		sb.append(mobile.substring(0, 3)).append("****").append(mobile.substring(mobile.length() - 4, mobile.length()));
		return sb.toString();
	}
	
	/**
	 * 获取银行卡前6后4位
	 *
	 * @param idCard
	 * @return
	 */
	public static String getBankCardFirst6Last4(String bankCard) {
		if (StringUtils.isEmpty(bankCard)) {
			return "";
		}
		if (bankCard.length() <= 10) {
			return bankCard;
		}
		StringBuffer sb = new StringBuffer();
		sb.append(bankCard.substring(0, 6)).append("****")
				.append(bankCard.substring(bankCard.length() - 4, bankCard.length()));
		return sb.toString();
	}
	
	/**
	 * 获取身份证前3后4位
	 *
	 * @param idCard
	 * @return
	 */
	public static String getIdCardFirst3Last4(String idCard) {
		if (StringUtils.isEmpty(idCard)) {
			return "";
		}
		if (idCard.length() <= 10) {
			return idCard;
		}
		StringBuffer sb = new StringBuffer();
		sb.append(idCard.substring(0, 3)).append("****").append(idCard.substring(idCard.length() - 4, idCard.length()));
		return sb.toString();
	}
}
